﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using v1_SweetGeeks.Controller;
using System.Data;
using System.Data.SqlClient;
using Google.Apis.Analytics.v3;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using Google.Apis.Services;

namespace v1_SweetGeeks.View
{
    public unsafe partial class MainWindow : Window
    {
        private controller con = new controller();
        private Thread mainThread;
        private string[] Months = {"NULL", "Januar", "Febuar", "Marts", "April", "Maj", "Juni", "Juli", "August", "September", "Oktober", "November", "December"};
        private double widthMultipler;
        private double heightMultipler;
        private double multipler;
        private int displayState;
        private TileInfo mainTile;
        private TileInfo mainExpTxt;
        private TileInfo mainCurTxt;
        private TileInfo mainLasTxt;
        private TileInfo mainExpMonth;
        private TileInfo mainCurMonth;
        private TileInfo mainLasMonth;
        private TileInfo txtMain;
        private TileInfo imgLogo;
        private TileInfo lasMonth365;
        private TileInfo curMonth365;
        private TileInfo expMonth365;
        private TileInfo las365Txt;
        private TileInfo cur365Txt;
        private TileInfo exp365Txt;
        private TileInfo toDoTxt;
        private TileInfo toDos;
        private TileInfo doingTxt;
        private TileInfo doings;
        private TileInfo testTxt;
        private TileInfo tests;
        private TileInfo doneTxt;
        private TileInfo dones;
        private TileInfo viewsTxt;
        private bool update;
        private DateTime t1;
        private bool showCollon;
        private bool run;
        private Thread waiter;
        private Size txtSize;
        private string txtDT;
        private string txtHH;
        private string txtMM;
        private double watchFont;
        private double *expectedMonthProfit;
        private double *currentMonthProfit;
        private double *lastMonthProfit;
        private double *expectedProductProfit;
        private double *currentProductProfit;
        private double *lastProductProfit;
        private uint *productStates;
        private int *productViews;
        private string currentProductName;
        private int lastMonthIterator;
        private int currentMonthIterator;
        private int nextMonthIterator;
        private bool locked;

        public MainWindow()
        {
            InitializeComponent();
            WindowState = System.Windows.WindowState.Maximized;
            run = true;
            watchFont = 30;
            lastMonthIterator = DateTime.Now.Month - 1 > 0 ? DateTime.Now.Month - 1 : 12;
            currentMonthIterator = DateTime.Now.Month;
            nextMonthIterator = DateTime.Now.Month + 1 <= 12 ? DateTime.Now.Month + 1 : 1;
            locked = false;

            try
            {
                con.InitializeModel();
            }
            catch(Exception myex)
            {
                MessageBox.Show(myex.ToString());
                CustomClose();
            }

            if(run)
            {
                expectedMonthProfit = con.BindToExpectedMonth();
                currentMonthProfit = con.BindToCurrentMonth();
                lastMonthProfit = con.BindToLastMonth();

                expectedProductProfit = con.BindToExpectedProductProfit();
                currentProductProfit = con.BindToCurrentProductProfit();
                lastProductProfit = con.BindToLastProductProfit();

                currentProductName = con.GetProductName();
                productStates = con.BindToProductState();
                productViews = con.BindToProductViews();

                Draw.Initialize(ref myCanvas, Brushes.White, 1.0);
                mainTile = new TileInfo(0.0, 0.0, 0.0, 0.0);
                txtMain = new TileInfo(0.0, 0.0, 0.0, 0.0);
                imgLogo = new TileInfo(0.0, 0.0, 0.0, 0.0);
                mainExpTxt = new TileInfo(0.0, 0.0, Months[nextMonthIterator] + "\n~" + GetValue(expectedMonthProfit) + ",-");
                mainCurTxt = new TileInfo(0.0, 0.0, Months[currentMonthIterator] + "\n" + GetValue(currentMonthProfit) + ",-");
                mainLasTxt = new TileInfo(0.0, 0.0, Months[lastMonthIterator] + "\n" + GetValue(lastMonthProfit) + ",-");
                mainCurMonth = new TileInfo(0.0, 0.0, 0.0, 0.0);
                mainExpMonth = new TileInfo(0.0, 0.0, 0.0, 0.0);
                mainLasMonth = new TileInfo(0.0, 0.0, 0.0, 0.0);
                lasMonth365 = new TileInfo(0.0, 0.0, 0.0, 0.0);
                curMonth365 = new TileInfo(0.0, 0.0, 0.0, 0.0);
                expMonth365 = new TileInfo(0.0, 0.0, 0.0, 0.0);
                las365Txt = new TileInfo(0.0, 0.0, Months[lastMonthIterator] + "\n" + GetValue(lastProductProfit) + ",-");
                cur365Txt = new TileInfo(0.0, 0.0, Months[currentMonthIterator] + "\n" + GetValue(currentProductProfit) + ",-");
                exp365Txt = new TileInfo(0.0, 0.0, Months[nextMonthIterator] + "\n~" + GetValue(expectedProductProfit) + ",-");
                toDoTxt = new TileInfo(0.0, 0.0, "Ikke startet");
                toDos = new TileInfo(0.0, 0.0, "0");
                doingTxt = new TileInfo(0.0, 0.0, "Igang");
                doings = new TileInfo(0.0, 0.0, "0");
                testTxt = new TileInfo(0.0, 0.0, "Leveret");
                tests = new TileInfo(0.0, 0.0, "0");
                doneTxt = new TileInfo(0.0, 0.0, "Færdig");
                dones = new TileInfo(0.0, 0.0, "0");
                viewsTxt = new TileInfo(0.0, 0.0, "Hits \n" + GetValue(productViews));

                mainTile.SetImage("wallpaper.png");
                imgLogo.SetImage(currentProductName + ".png");
                txtMain.SetImage("Geek.png");
                mainExpTxt.opacity = 0.5;
                mainExpMonth.brush = (Brush)new BrushConverter().ConvertFromString("#00C6EE");
                mainExpMonth.opacity = 0.5;
                mainCurMonth.brush = (Brush)new BrushConverter().ConvertFromString("#00C6EE");
                mainLasMonth.brush = (Brush)new BrushConverter().ConvertFromString("#00C6EE");
                lasMonth365.brush = (Brush)new BrushConverter().ConvertFromString("#00C6EE");
                curMonth365.brush = (Brush)new BrushConverter().ConvertFromString("#00C6EE");
                expMonth365.brush = (Brush)new BrushConverter().ConvertFromString("#00C6EE");
                expMonth365.opacity = 0.5;
                exp365Txt.opacity = 0.5;

                Tile.tiles.Add(mainTile);
                Tile.tiles.Add(toDoTxt);
                Tile.tiles.Add(toDos);
                Tile.tiles.Add(doingTxt);
                Tile.tiles.Add(doings);
                Tile.tiles.Add(testTxt);
                Tile.tiles.Add(tests);
                Tile.tiles.Add(doneTxt);
                Tile.tiles.Add(dones);
                Tile.tiles.Add(mainExpMonth);
                Tile.tiles.Add(mainCurMonth);
                Tile.tiles.Add(mainLasMonth);
                Tile.tiles.Add(imgLogo);
                Tile.tiles.Add(txtMain);
                Tile.tiles.Add(mainExpTxt);
                Tile.tiles.Add(mainCurTxt);
                Tile.tiles.Add(mainLasTxt);
                Tile.tiles.Add(lasMonth365);
                Tile.tiles.Add(curMonth365);
                Tile.tiles.Add(expMonth365);
                Tile.tiles.Add(las365Txt);
                Tile.tiles.Add(cur365Txt);
                Tile.tiles.Add(exp365Txt);
                Tile.tiles.Add(viewsTxt);
            
                txtDT = DateTime.Now.ToShortDateString();
                txtHH = DateTime.Now.ToShortTimeString().Split(':')[0];
                txtMM = DateTime.Now.ToShortTimeString().Split(':')[1];
                Draw.DrawText(Draw.CanvasWidth(199), Draw.CanvasHeight(3), 24, txtHH + "__", Brushes.White, 0.0, out txtSize);

                displayState = 0;
                update = true;

                mainThread = new Thread(Dispatch);
                mainThread.Name = "MainWindowThread";
                mainThread.IsBackground = true;
                mainThread.Start();
            }
        }

        private void Dispatch()
        {
            while(run)
            {
                //Allows access for GUI elements
                Dispatcher.Invoke(DispatcherPriority.Render, new ThreadStart(() => DrawToGUI()));
                Thread.Sleep((int)(1000.0/60.0)); //60 FPS
            }
        }

        private void UpdateDashBoard()
        {
            widthMultipler = Draw.myCanvas.ActualWidth / 1920;
            heightMultipler = Draw.myCanvas.ActualHeight / 1080;
            multipler = widthMultipler < heightMultipler ? widthMultipler : heightMultipler;
            double crop = 1.0;
            if(mainTile.img != null)
            {
                crop = Draw.myCanvas.ActualWidth / mainTile.img.Source.Width;
                crop = Draw.myCanvas.ActualHeight / mainTile.img.Source.Height > crop ? Draw.myCanvas.ActualHeight / mainTile.img.Source.Height : crop;
            }
                
            mainExpTxt.text = Months[nextMonthIterator] + "\n~" + GetValue(expectedMonthProfit) + ",-";
            mainCurTxt.text = Months[currentMonthIterator] + "\n" + GetValue(currentMonthProfit) + ",-";
            mainLasTxt.text = Months[lastMonthIterator] + "\n" + GetValue(lastMonthProfit) + ",-";

            las365Txt.text = Months[lastMonthIterator] + "\n" + GetValue(lastProductProfit) + ".-";
            cur365Txt.text = Months[currentMonthIterator] + "\n" + GetValue(currentProductProfit) + ".-";
            exp365Txt.text = Months[nextMonthIterator] + "\n~" + GetValue(expectedProductProfit) + ".-";

            if(displayState == 3) // Product Switch
            {
                mainTile.Setdestination(Draw.CanvasWidth(0), Draw.CanvasHeight(0), mainTile.img.Source.Width * crop, mainTile.img.Source.Height * crop);
                txtMain.Setdestination(Draw.CanvasWidth(1), Draw.CanvasHeight(2), Draw.CanvasWidth(11), Draw.CanvasHeight(12));
                    
                mainExpTxt.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(77 - ScaleGraph(GetValue(expectedMonthProfit), 70, false)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                mainCurTxt.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(77 - ScaleGraph(GetValue(currentMonthProfit), 70, false)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                mainLasTxt.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(77 - ScaleGraph(GetValue(lastMonthProfit), 70, false)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                    
                mainExpMonth.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(89 - ScaleGraph(GetValue(expectedMonthProfit), 70, false)), Draw.CanvasWidth(75), Draw.CanvasHeight(89));
                mainCurMonth.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(89 - ScaleGraph(GetValue(currentMonthProfit), 70, false)), Draw.CanvasWidth(55), Draw.CanvasHeight(89));
                mainLasMonth.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(89 - ScaleGraph(GetValue(lastMonthProfit), 70, false)), Draw.CanvasWidth(35), Draw.CanvasHeight(89));

                imgLogo.Setdestination(Draw.CanvasWidth(1), Draw.CanvasHeight(92), Draw.CanvasWidth(1), Draw.CanvasHeight(98));
            }
            if(displayState == 2) // Dash Board shown
            {
                mainTile.Setdestination(Draw.CanvasWidth(0), Draw.CanvasHeight(0), mainTile.img.Source.Width * crop, mainTile.img.Source.Height * crop);
                txtMain.Setdestination(Draw.CanvasWidth(1), Draw.CanvasHeight(2), Draw.CanvasWidth(11), Draw.CanvasHeight(12));
                    
                mainExpTxt.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(77 - ScaleGraph(GetValue(expectedMonthProfit), 70, false)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                mainCurTxt.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(77 - ScaleGraph(GetValue(currentMonthProfit), 70, false)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                mainLasTxt.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(77 - ScaleGraph(GetValue(lastMonthProfit), 70, false)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                    
                mainExpMonth.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(89 - ScaleGraph(GetValue(expectedMonthProfit), 70, false)), Draw.CanvasWidth(75), Draw.CanvasHeight(89));
                mainCurMonth.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(89 - ScaleGraph(GetValue(currentMonthProfit), 70, false)), Draw.CanvasWidth(55), Draw.CanvasHeight(89));
                mainLasMonth.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(89 - ScaleGraph(GetValue(lastMonthProfit), 70, false)), Draw.CanvasWidth(35), Draw.CanvasHeight(89));

                imgLogo.Setdestination(Draw.CanvasWidth(47), Draw.CanvasHeight(92), Draw.CanvasWidth(53), Draw.CanvasHeight(98));

                expMonth365.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(92), Draw.CanvasWidth(75), Draw.CanvasHeight(92));
                curMonth365.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(92), Draw.CanvasWidth(55), Draw.CanvasHeight(92));
                lasMonth365.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(92), Draw.CanvasWidth(35), Draw.CanvasHeight(92));

                exp365Txt.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                cur365Txt.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                las365Txt.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));

                toDoTxt.Setdestination(Draw.CanvasWidth(12), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                doingTxt.Setdestination(Draw.CanvasWidth(37), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                testTxt.Setdestination(Draw.CanvasWidth(62), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                doneTxt.Setdestination(Draw.CanvasWidth(87), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));

                toDos.Setdestination(Draw.CanvasWidth(13), Draw.CanvasHeight(278), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                doings.Setdestination(Draw.CanvasWidth(38), Draw.CanvasHeight(278), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                tests.Setdestination(Draw.CanvasWidth(63), Draw.CanvasHeight(278), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                dones.Setdestination(Draw.CanvasWidth(88), Draw.CanvasHeight(278), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                
                viewsTxt.Setdestination(Draw.CanvasWidth(0), Draw.CanvasHeight(278), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
            }
            if(displayState == 1) // Product Shown
            {
                mainTile.Setdestination(Draw.CanvasWidth(0), Draw.CanvasHeight(0), mainTile.img.Source.Width * crop, mainTile.img.Source.Height * crop);
                txtMain.Setdestination(Draw.CanvasWidth(2), Draw.CanvasHeight(2), Draw.CanvasWidth(8), Draw.CanvasHeight(8));

                mainLasTxt.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(-100), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                mainExpTxt.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(-100), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                mainCurTxt.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(-100), Draw.CanvasWidth(0), Draw.CanvasHeight(0));

                mainExpMonth.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(8), Draw.CanvasWidth(75), Draw.CanvasHeight(8));
                mainCurMonth.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(8), Draw.CanvasWidth(55), Draw.CanvasHeight(8));
                mainLasMonth.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(8), Draw.CanvasWidth(35), Draw.CanvasHeight(8));

                imgLogo.Setdestination(Draw.CanvasWidth(1), Draw.CanvasHeight(10), Draw.CanvasWidth(15), Draw.CanvasHeight(24));

                expMonth365.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(70 - ScaleGraph(GetValue(expectedProductProfit), 45, true)), Draw.CanvasWidth(75), Draw.CanvasHeight(70));
                curMonth365.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(70 - ScaleGraph(GetValue(currentProductProfit), 45, true)), Draw.CanvasWidth(55), Draw.CanvasHeight(70));
                lasMonth365.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(70 - ScaleGraph(GetValue(lastProductProfit), 45, true)), Draw.CanvasWidth(35), Draw.CanvasHeight(70));

                exp365Txt.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(58 - ScaleGraph(GetValue(expectedProductProfit), 45, true)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                cur365Txt.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(58 - ScaleGraph(GetValue(currentProductProfit), 45, true)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                las365Txt.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(58 - ScaleGraph(GetValue(lastProductProfit), 45, true)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                    
                toDoTxt.Setdestination(Draw.CanvasWidth(12), Draw.CanvasHeight(72), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                doingTxt.Setdestination(Draw.CanvasWidth(37), Draw.CanvasHeight(72), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                testTxt.Setdestination(Draw.CanvasWidth(62), Draw.CanvasHeight(72), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                doneTxt.Setdestination(Draw.CanvasWidth(87), Draw.CanvasHeight(72), Draw.CanvasWidth(0), Draw.CanvasHeight(0));

                toDos.Setdestination(Draw.CanvasWidth(13), Draw.CanvasHeight(78), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                toDos.text = GetProductStates()[0].ToString();
                doings.Setdestination(Draw.CanvasWidth(38), Draw.CanvasHeight(78), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                doings.text = GetProductStates()[1].ToString();
                tests.Setdestination(Draw.CanvasWidth(63), Draw.CanvasHeight(78), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                tests.text = GetProductStates()[2].ToString();
                dones.Setdestination(Draw.CanvasWidth(88), Draw.CanvasHeight(78), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                dones.text = GetProductStates()[3].ToString();
                
                viewsTxt.Setdestination(Draw.CanvasWidth(1), Draw.CanvasHeight(30), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                viewsTxt.text = "Sessions \n" + GetValue(productViews) + "\nLast 3 days";
            }
            if(displayState == 0) // Dash Board shown
            {
                if(mainTile.img != null)
                {
                    mainTile.Setdestination(Draw.CanvasWidth(0), Draw.CanvasHeight(0), mainTile.img.Source.Width * crop, mainTile.img.Source.Height * crop);
                }
                else
                {
                    mainTile.Setdestination(Draw.CanvasWidth(0), Draw.CanvasHeight(0), 1920 * multipler, 1080 * multipler);
                }
                txtMain.Setdestination(Draw.CanvasWidth(1), Draw.CanvasHeight(2), Draw.CanvasWidth(11), Draw.CanvasHeight(12));
                    
                mainExpTxt.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(77 - ScaleGraph(GetValue(expectedMonthProfit), 70, false)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                mainCurTxt.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(77 - ScaleGraph(GetValue(currentMonthProfit), 70, false)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                mainLasTxt.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(77 - ScaleGraph(GetValue(lastMonthProfit), 70, false)), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                    
                mainExpMonth.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(89 - ScaleGraph(GetValue(expectedMonthProfit), 70, false)), Draw.CanvasWidth(75), Draw.CanvasHeight(89));
                mainCurMonth.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(89 - ScaleGraph(GetValue(currentMonthProfit), 70, false)), Draw.CanvasWidth(55), Draw.CanvasHeight(89));
                mainLasMonth.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(89 - ScaleGraph(GetValue(lastMonthProfit), 70, false)), Draw.CanvasWidth(35), Draw.CanvasHeight(89));

                imgLogo.Setdestination(Draw.CanvasWidth(47), Draw.CanvasHeight(92), Draw.CanvasWidth(53), Draw.CanvasHeight(98));

                expMonth365.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(92), Draw.CanvasWidth(75), Draw.CanvasHeight(92));
                curMonth365.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(92), Draw.CanvasWidth(55), Draw.CanvasHeight(92));
                lasMonth365.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(92), Draw.CanvasWidth(35), Draw.CanvasHeight(92));

                exp365Txt.Setdestination(Draw.CanvasWidth(65), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                cur365Txt.Setdestination(Draw.CanvasWidth(45), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                las365Txt.Setdestination(Draw.CanvasWidth(25), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                    
                toDoTxt.Setdestination(Draw.CanvasWidth(12), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                doingTxt.Setdestination(Draw.CanvasWidth(37), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                testTxt.Setdestination(Draw.CanvasWidth(62), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                doneTxt.Setdestination(Draw.CanvasWidth(87), Draw.CanvasHeight(200), Draw.CanvasWidth(0), Draw.CanvasHeight(0));

                toDos.Setdestination(Draw.CanvasWidth(13), Draw.CanvasHeight(278), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                doings.Setdestination(Draw.CanvasWidth(38), Draw.CanvasHeight(278), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                tests.Setdestination(Draw.CanvasWidth(63), Draw.CanvasHeight(278), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
                dones.Setdestination(Draw.CanvasWidth(88), Draw.CanvasHeight(278), Draw.CanvasWidth(0), Draw.CanvasHeight(0));

                viewsTxt.Setdestination(Draw.CanvasWidth(0), Draw.CanvasHeight(278), Draw.CanvasWidth(0), Draw.CanvasHeight(0));
            }
        }

        private void DrawToGUI()
        {
            for(int i = 0; i < Tile.tiles.Count; i++)
            {
                Tile.tiles[i].Update();
            }

            bool UpdateForTheWatch = false;
            if((DateTime.Now - t1).TotalMilliseconds > 500.0)
            {
                showCollon = !showCollon;
                t1 = DateTime.Now;
                UpdateForTheWatch = true;
            }

            if(update)
            {
                Draw.myCanvas.Children.Clear();
                UpdateDashBoard();

                if(displayState == 1)
                {
                    waiter = new Thread(() => StateSet(9500));
                    waiter.Start();
                }
                else
                {
                    waiter = new Thread(() => StateSet(3500));
                    waiter.Start();
                }
                
                if(multipler > 0)
                {
                    txtDT = DateTime.Now.ToShortDateString();
                    txtHH = DateTime.Now.ToShortTimeString().Split(':')[0];
                    txtMM = DateTime.Now.ToShortTimeString().Split(':')[1];
                    Draw.DrawText(Draw.CanvasWidth(199), Draw.CanvasHeight(3), watchFont * multipler, txtHH + "__", Brushes.White, 0.0, out txtSize);
                }
                
                for(int i = 0; i < Tile.tiles.Count; i++)
                {
                    if(!Tile.tiles[i].is_image && !Tile.tiles[i].is_text)
                    {
                        Draw.DrawRectangle(Tile.tiles[i].x1, Tile.tiles[i].y1, Tile.tiles[i].x2, Tile.tiles[i].y2, Tile.tiles[i].brush, Tile.tiles[i].opacity - 0.15);
                    }
                    else if(Tile.tiles[i].is_image)
                    {
                        Draw.DrawImage(Tile.tiles[i].x1, Tile.tiles[i].y1, Tile.tiles[i].x2, Tile.tiles[i].y2, ref Tile.tiles[i].img);
                    }
                    else if(Tile.tiles[i].is_text)
                    {
                        string text = Tile.tiles[i].text;
                        if(Tile.tiles[i].text.Contains(','))
                        {
                            text = Tile.tiles[i].text.Split(',')[0] + ",-";
                        }
                        Draw.DrawText(Tile.tiles[i].x1, Tile.tiles[i].y1, 46 * multipler, text, Tile.tiles[i].brush, Tile.tiles[i].opacity);
                    }
                }
                
                if(showCollon)
                {
                    Draw.DrawText(Draw.CanvasWidth(89), Draw.CanvasHeight(1), watchFont * multipler, txtDT, Brushes.White, 1.0);
                    Draw.DrawText(Draw.CanvasWidth(89), Draw.CanvasHeight(4), watchFont * multipler, txtHH + " :", Brushes.White, 1.0);
                    Draw.DrawText(Draw.CanvasWidth(89) + (txtSize.Width-2), Draw.CanvasHeight(4), watchFont * multipler, txtMM, Brushes.White, 1.0);
                }
                else
                {
                    Draw.DrawText(Draw.CanvasWidth(89), Draw.CanvasHeight(1), watchFont * multipler, txtDT, Brushes.White, 1.0);
                    Draw.DrawText(Draw.CanvasWidth(89), Draw.CanvasHeight(4), watchFont * multipler, txtHH + "  ", Brushes.White, 1.0);
                    Draw.DrawText(Draw.CanvasWidth(89) + (txtSize.Width-2), Draw.CanvasHeight(4), watchFont * multipler, txtMM, Brushes.White, 1.0);
                }

                update = false;
            }
            
            if(Tile.Moving() || UpdateForTheWatch)
            {
                Draw.myCanvas.Children.Clear();
                
                for(int i = 0; i < Tile.tiles.Count; i++)
                {
                    if(!Tile.tiles[i].is_image && !Tile.tiles[i].is_text)
                    {
                        Draw.DrawRectangle(Tile.tiles[i].x1, Tile.tiles[i].y1, Tile.tiles[i].x2, Tile.tiles[i].y2, Tile.tiles[i].brush, Tile.tiles[i].opacity - 0.15);
                    }
                    else if(Tile.tiles[i].is_image)
                    {
                        Draw.DrawImage(Tile.tiles[i].x1, Tile.tiles[i].y1, Tile.tiles[i].x2, Tile.tiles[i].y2, ref Tile.tiles[i].img);
                    }
                    else if(Tile.tiles[i].is_text)
                    {
                        string text = Tile.tiles[i].text;
                        if(Tile.tiles[i].text.Contains(','))
                        {
                            text = Tile.tiles[i].text.Split(',')[0] + ",-";
                        }
                        Draw.DrawText(Tile.tiles[i].x1, Tile.tiles[i].y1, 46 * multipler, text, Tile.tiles[i].brush, Tile.tiles[i].opacity);
                    }
                }
                
                if(showCollon)
                {
                    Draw.DrawText(Draw.CanvasWidth(89), Draw.CanvasHeight(1), watchFont * multipler, txtDT, Brushes.White, 1.0);
                    Draw.DrawText(Draw.CanvasWidth(89), Draw.CanvasHeight(4), watchFont * multipler, txtHH + " :", Brushes.White, 1.0);
                    Draw.DrawText(Draw.CanvasWidth(89) + (txtSize.Width-2), Draw.CanvasHeight(4), watchFont * multipler, txtMM, Brushes.White, 1.0);
                }
                else
                {
                    Draw.DrawText(Draw.CanvasWidth(89), Draw.CanvasHeight(1), watchFont * multipler, txtDT, Brushes.White, 1.0);
                    Draw.DrawText(Draw.CanvasWidth(89), Draw.CanvasHeight(4), watchFont * multipler, txtHH + "  ", Brushes.White, 1.0);
                    Draw.DrawText(Draw.CanvasWidth(89) + (txtSize.Width-2), Draw.CanvasHeight(4), watchFont * multipler, txtMM, Brushes.White, 1.0);
                }
            }
        }

        private void StateSet(int milliseconds)
        {
            for(int i = 0; i < milliseconds && run; i+=100)
            {
                Thread.Sleep(100);
            }

            displayState = displayState < 3 ? displayState + 1 : 0;

            if(displayState == 0)
            {
                con.IterateProductIterator();
                con.InitializeModel();
                currentProductName = con.GetProductName();
                imgLogo.SetImage(currentProductName + ".png");
            }
            else
            {
                con.InitializeModel();
            }

            update = true;
        }

        private double ScaleGraph(double current, double height, bool scaleForProduct)
        {
            double result = 0.0;

            do
            {
                if(!locked)
                {
                    if(scaleForProduct)
                    {
                        double max = 0.0;

                        double temp = GetValue(lastProductProfit);
                        if(temp > max)
                        {
                            max = temp; 
                        }
                        temp = GetValue(currentProductProfit);
                        if(temp > max)
                        {
                            max = temp; 
                        }
                        temp = GetValue(expectedProductProfit);
                        if(temp > max)
                        {
                            max = temp; 
                        }

                        double multipler = height / max;

                        result = max > 0.0 ? current * multipler : 0.0;
                    }
                    else
                    {
                        double max = 0.0;

                        double temp = GetValue(lastMonthProfit);
                        if(temp > max)
                        {
                            max = temp; 
                        }
                        temp = GetValue(currentMonthProfit);
                        if(temp > max)
                        {
                            max = temp; 
                        }
                        temp = GetValue(expectedMonthProfit);
                        if(temp > max)
                        {
                            max = temp; 
                        }

                        double multipler = height / max;

                        result = max > 0.0 ? current * multipler : 0.0;
                    }
                }
            }while(locked);

            return result;
        }

        private double GetValue(double *ptr)
        {
            double result = 0.0;
            
            do
            {
                if(!locked)
                {
                    locked = true;
                    if(ptr != null)
                    {
                        result = *ptr;
                    }
                    locked = false;
                }
            }while(locked);

            return result;
        }

        private double GetValue(int *ptr)
        {
            int result = 0;
            
            do
            {
                if(!locked)
                {
                    locked = true;
                    if(ptr != null)
                    {
                        result = *ptr;
                    }
                    locked = false;
                }
            }while(locked);

            return result;
        }

        private byte[] GetProductStates()
        {
            byte[] result = new byte[4];
            
            do
            {
                if(!locked)
                {
                    int pos = 0;
                    uint tempStates = *productStates;
                    for(int i = 0; i < 4; i++)
                    {
                        result[i] = (byte)(tempStates >> pos);
                        pos += 8;
                    }
                }
            }while(locked);

            return result;
        }

        private void mainWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.F11)
            {
                WindowState = WindowState == System.Windows.WindowState.Maximized ? System.Windows.WindowState.Normal : System.Windows.WindowState.Maximized;
            }

            if(e.Key == Key.Escape)
            {
                CustomClose();
            }
        }

        private void CustomClose()
        {
            run = false;
            try
            {
                waiter.Abort();
            }
            catch
            {
            }

            try
            {
                mainThread.Abort();
            }
            catch
            {
            }

            try
            {
                this.Close();
            }
            catch
            {
            }
        }
    }
}
